'use strict';
var path = require('path');
/**
 * electron 主文件
 */
const { app, BrowserWindow, ipcMain, Notification } = require('electron');

let win;
function createWin() {
  // 创建浏览器窗口
  win = new BrowserWindow({
    width: 800,
    height: 600,
    useContentSize: true,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegrationInWorker: true,
      nodeIntegration: true,
      contextIsolation: false,
    },
  });

  const URL = app.isPackaged
    ? path.join(__dirname, '../dist/index.html') // vite 构建后的静态文件地址
    : `http://localhost:${process.env.PORT || 9000}`; // vite 启动的服务器地址
  app.isPackaged ? win?.loadFile(URL) : win?.loadURL(URL);
  ipcMain.on("test", (event, msg) => {
    let myNotification = new Notification({
      title:msg,
      body: `收到来自渲染线程的信息`
    })

    myNotification.onclick = () => {
      console.log('通知被点击后触发')
    }
    myNotification.show()
  })
  win.webContents.on('did-finish-load', () => {
    win.webContents.send('ping', '发送信息到webvie')

  })
  if (!app.isPackaged) {
    //打开调试工具
    win.webContents.openDevTools();
  }
}
app.whenReady().then(() => {
  app.on('activate', () => {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWin();
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit();
});

/*此处为安装vue-devtools 扩展,vue3的vue-devtools需要使用vue-devtools beta版本
 官方文档 http://www.electronjs.org/docs/tutorial/devtools-extension
 安装之后若提示警告，可更改vue-devtools项目中的manifest.json，删除掉与报错内容有关的内容即可
*/
app.on('ready', async () => {
  createWin();
});